<?php
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Mensajes
 *
 * @author boscaru
 * @export Modules
 * @export-metadata RequiredRoles:ROLE_PROFESSOR
 */

Loader::using("ext::libs::FactoryEmail::SmtpMail");

class Mensajes extends Module {

    private $emails = array();
    private $subject = "";
    private $message = "";

    /**
     */
    public function compose() {
        $this->emails = $this->request->post("Emails");
        $Estudiantes = $this->request->post("Estudiantes");
        
        $Info = array();
        $ListaEstudiantes = array();
        
        foreach ($this->emails as $email){ 
            $Datos = explode(',', $email);
            $Datos[2] = trim($Datos[2],'}');
            array_push($Info, $Datos);
        }
        
        foreach ($Estudiantes as $estudiante){ 
            $Lista = explode(',', $estudiante);
            $Lista[2] = trim($Lista[2],'}');
            array_push($ListaEstudiantes, $Lista);
        }

        $this->assign("datos", $Info);
        $this->assign("emails", $this->processEmails($this->emails));
        $this->assign("listaEstudiantes", $ListaEstudiantes);
        $this->assign("action", $this->route->action("send"));
        $this->assign("cancel", $this->route->moduleAction("MisCursos", "index"));

        $this->display("compose");
    }

    public function index() {
        $this->compose();
    }

    /**
     * @export-metadata Bind:this
     */
    public function send() {
        if ($this->request->submitted()) {
            $enviado = false;
            
            
            $this->emails = $this->request->post("Emails");
            $this->message = $this->request->post("message");
            $this->subject = $this->request->post("subject");
            
            $Estudiante = $this->model->students->GetById($this->currentUser->getName());
            $Estudiante = $Estudiante->getNombres().' '.$Estudiante->getApellidos();
            
            $Mail = new SmtpMail($this->message, $this->subject, $this->emails, "smtp.gmail.com", "manlioferreira@gmail.com", "loquesea","", $Estudiante);
            $enviado = $Mail->SendMail();
            

            if ($enviado) {
                $this->addMessage(new Message("Mensaje enviado con &eacute;xito.", "", Message::SUCCESS));
            } else {
                $this->addMessage(new Message("No se pudo enviar el mensaje.", "Ups", Message::WARNING));
            }
        }
        $this->assign("regresar", $this->route->moduleAction("ListaEstudiante", "index"));
        
        $this->display();
    }

    public function getEmails() {
        return $this->emails;
    }

    public function setEmails($emails) {
        $this->emails = $emails;
    }

    public function getSubject() {
        return $this->subject;
    }

    public function setSubject($subject) {
        $this->subject = $subject;
    }

    public function getMessage() {
        return $this->message;
    }

    public function setMessage($message) {
        $this->message = $message;
    }

    private function processEmails(&$emails) {
        $result = array();

        if (is_array($emails)) {
            foreach ($emails as $email) {
                $splitted = explode(" ", $email, 2);
                $result[] = $splitted;
            }
        }

        return $result;
    }

}

?>
